<!--
 * @Author: Bennty
 * @Date: 2024-07-24 10:48:53
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2024-09-19 14:25:47
 * @Description: 请填写简介
-->
<template>
    <el-dialog v-model="props.visible" title="采购福利" width="25%" @close="handleClose">
        <el-form :model="formData" ref="formRef">
            <el-form-item label="产品名称">
                <span>{{ formData.welfareName }}</span>
            </el-form-item>
            
            <el-form-item label="申请采购量" prop="applyNumber">
                <el-input v-model.number="formData.applyNumber" placeholder="请输入"></el-input>
            </el-form-item>
            <el-form-item label="申请采购备注" prop="remark">
                <el-input v-model="formData.remark" type="textarea" limit="125" show-limit placeholder="请输入"></el-input>
            </el-form-item>   
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button  type="primary" @click="handleSubmit">确 定</el-button>
                <el-button @click="handleClose">取 消</el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type { FormInstance } from 'element-plus';
import { addWelFarePurchaseApplyApi } from '@/api/sj/welfare-crm/index';


const props = defineProps(['visible','data']);
const emits = defineEmits(['close','submit']);
const formRef = ref<FormInstance>();

const data = reactive({
    formData:{
        welfareId: undefined,
        welfareName: undefined,
        applyNumber: undefined,
        remark: undefined
    }
});
const { formData } = toRefs(data);

watch(()=>props.data,(newData)=>{
    // Object.assign(formData.value, newData);
    formData.value.welfareName = newData.welfareName;
    formData.value.welfareId = newData.id;
},{deep:true});

const handleSubmit = async () => {
    const { code } = await addWelFarePurchaseApplyApi(formData.value);
    if(code == 200){
        emits('submit', true);
        handleClose();
    }else{
        emits('submit', false);
    }
};

const handleClose = ()=>{
    formRef.value?.resetFields();
    formData.value.welfareName = undefined;
    formData.value.welfareId = undefined;
    emits('close');
};

</script>

<style lang="scss" scoped>
.dialog-footer{
    display: flex;
    justify-content: center;
}
</style>